<?php
namespace app\api\controller;
use app\common\controller\CommonApi;

/**
 * 评论、点赞相关
 * 尽量少查询
 */
class Comment extends CommonApi{
    protected $tn = 'common/comment';


    /**
     * [addComment 添加评论]  
     * 
     */
    public function addComment(){
        $validate = validate($this->tn);
        if($validate->check($this->post) === false) {
            $this->apiReturn(0,$validate->getError());
        }
        db()->startTrans();
        //评论别人发布的评论
        if(isset($this->post['comment_id']) &&  intval($this->post['comment_id'])){
            $res = model($this->tn)->get(intval($this->post['comment_id']));
            if(is_null($res)){
                $this->apiReturn(0,'该评论不存在或已被删除');
            }
            if(!$res->setInc('comment_count')){
                db()->rollback();
                $this->apiReturn(0,'评论失败，请稍后再试');
            }
        }
        $this->post['user_id'] = UID;
        if(!model('common/comment')->allowField(true)->save($this->post)){
            db()->rollback();
            $this->apiReturn(0,'评论失败,请稍后再试1');
        }
        db()->commit();
        $this->apiReturn(1,'评论成功');
      
    }

    

    /**
     * [addZan 评论点赞]
     * 
     */
    public function addZan(){
        if(!isset($this->post['id']) || intval($this->post['id']) <= 0){
            $this->apiReturn(0,'参数id丢失');
        }
        $model = model($this->tn)->get(intval($this->post['id']));
        if(is_null($model)){
            $this->apiReturn(0,'该评论不存在或已被删除');
        }
        //是否点过赞
        if(db('comment_zan')->where('user_id',UID)->where('comment_id',intval($this->post['id']))->find()){
            $this->apiReturn(1,'点赞成功');
        }
        db()->startTrans();
        $res = db('comment_zan')->insert([
            'user_id' => UID,
            'comment_id' => intval($this->post['id']),
            'add_time' => time()
        ]);
        if(!$res){
            db()->rollback();
            $this->apiReturn(0,'点赞失败，请稍后再试');
        }
        if(!$model->setInc('zan_count')){
            db()->rollback();
            $this->apiReturn(0,'点赞失败，请稍后再试');
        }
        db()->commit();
        $this->apiReturn(1,'点赞成功');
    }

    //取消点赞
    public function offZan(){
        if(!isset($this->post['id']) || intval($this->post['id']) <= 0){
            $this->apiReturn(0,'参数id丢失');
        }
        $model = model($this->tn)->get(intval($this->post['id']));
        if(is_null($model)){
           $this->apiReturn(0,'该评论不存在或已被删除');
        }
        //是否点过赞
        if(!db('comment_zan')->where('user_id',UID)->where('comment_id',intval($this->post['id']))->find()){
            $this->apiReturn(1,'取消点赞成功');
        }
        db()->startTrans();
      
        if(!db('comment_zan')->where('user_id',UID)->where('comment_id',intval($this->post['id']))->delete()){
            db()->rollback();
            $this->apiReturn(0,'操作失败，请稍后再试');
        }
        if(!$model->setDec('zan_count')){
            db()->rollback();
            $this->apiReturn(0,'操作失败，请稍后再试');
        }
        db()->commit();
        $this->apiReturn(1,'取消成功');
    }

    //删除评论
    public function delComment(){
        if(!isset($this->post['id']) || intval($this->post['id']) <= 0){
            $this->apiReturn(0,'参数id丢失');
        }
        $model = model($this->tn)->get(intval($this->post['id']));
        if(is_null($model)){
            $this->apiReturn(0,'该评论不存在或已被删除');
        }

        if($model['user_id'] != UID){
            $this->apiReturn(0,'不能删除别人的评论');
        }

        if(!$model->delete()){
            $this->apiReturn(0,'删除失败，请稍后再试');
        }
        $this->apiReturn(1,'删除成功');
    }

    //发布点赞
    public function issueZan(){
        $validate = new \think\validate([
            'foreign_key' => 'require',
            'type' => 'require|in:1,2,3'
        ],[
            'foreign_key.require' => '参数foreign_key丢失',
            'type.require' => '参数type丢失',
            'type.in' => '参数type有误'
        ]);

        if($validate->check($this->post) === false){
            $this->apiReturn(0,$validate->getError());
        }

        if(db('issue_zan')->where('user_id',UID)->where('foreign_key',intval($this->post['foreign_key']))->where('type',intval($this->post['type']))->find()){
            $this->apiReturn(1,'点赞成功');
        }

        if(!db('issue_zan')->insert([
            'user_id' => UID,
            'foreign_key' => intval($this->post['foreign_key']),
            'type' => intval($this->post['type']),
            'add_time' => time()
        ])){
            $this->apiReturn(0,'操作失败，请稍后再试');
        }

        $this->apiReturn(1,'点赞成功');
    }

    //取消发布点赞
    public function issueOffZan(){
        $validate = new \think\validate([
            'foreign_key' => 'require',
            'type' => 'require|in:1,2,3'
        ],[
            'foreign_key.require' => '参数foreign_key丢失',
            'type.require' => '参数type丢失',
            'type.in' => '参数type有误'
        ]);

        if($validate->check($this->post) === false){
            $this->apiReturn(0,$validate->getError());
        }

        if(!db('issue_zan')->where('user_id',UID)->where('foreign_key',intval($this->post['foreign_key']))->where('type',intval($this->post['type']))->find()){
            $this->apiReturn(1,'取消点赞成功');
        }

        if(!db('issue_zan')->where('user_id',UID)->where('foreign_key',intval($this->post['foreign_key']))->where('type',intval($this->post['type']))->delete()){
            $this->apiReturn(0,'操作失败，请稍后再试');
        }

        $this->apiReturn(1,'取消点赞成功');
    }


    public function myComment(){

    }

    public function myZan(){
        
    }
}
